package cn.bjsxt.plane;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;

import cn.bjsxt.util.Constant;
import cn.bjsxt.util.GameUtil;
import cn.bjsxt.util.MyFrame;

public class PlaneGameFrame extends MyFrame {
	Image bg = GameUtil.getImage("images/plane-bg.png");
	Plane plane = new Plane("images/plane-1.png", 50, 50);
	
	ArrayList bulletList = new ArrayList();   //范型暂时未学，暂不加
	
	
	Date startTime;
	Date endTime;
	
	Explode baozha;
	
	public void paint(Graphics g) {
		g.drawImage(bg,  0,  0,  null);
		plane.draw(g);
		
		for (int i = 0; i < bulletList.size(); i++) {
			Bullet b = (Bullet) bulletList.get(i);
			b.draw(g);
			
			//检测跟飞机的碰撞
			boolean peng = b.getRect().intersects(plane.getRect());
			if (peng) {
				System.out.println("碰撞");
				if (baozha == null) {
					baozha = new Explode(plane.x, plane.y);
					endTime = new Date();
					plane.setLive(false); //飞机死掉
				}
				
				baozha.draw(g);
				break;
			}
			
			if (!plane.isLive()) {
				printInfo(g, "GAME OVER", 50, 100, 200, Color.white);
				int period = (int)((endTime.getTime() - startTime.getTime()) / 1000);
				printInfo(g, "时间：" + period + "秒", 20, 120, 300, Color.white);
				
				switch (period / 10) {
					case 0:
					case 1:
						printInfo(g, "失败", 40, 120, 260, Color.white);
						break;
					case 2:
					case 3:
						printInfo(g, "及格", 40, 120, 260, Color.white);
						break;
					case 4:
					case 5:
						printInfo(g, "良好", 40, 120, 260, Color.white);
						break;
					default:
						printInfo(g, "优秀", 40, 120, 260, Color.white);
						break;
				}
			}
		}
	}
	
	public void printInfo(Graphics g, String str, int size, int x, int y, Color color) {
		Color c = g.getColor();
		g.setColor(color);
		Font f = new Font("宋体", Font.BOLD, size);
		g.setFont(f);
		g.drawString(str, x, y);
		g.setColor(c);
	}
	
	public static void main(String[] args) {
		new PlaneGameFrame().launchFrame();
	}
	
	public void launchFrame() {
		super.launchFrame();
		//增加键盘的监听
		
		addKeyListener(new KeyMonitor());
		
		//生成一堆子弹
		for(int i = 0; i < 30; i++) {
			Bullet b = new Bullet();
			bulletList.add(b);
		}
		
		startTime = new Date();
	}
	
	// 定义为内部类，可以放鞭使用外部类的普通属性
	class KeyMonitor extends KeyAdapter {
		
		public void keyPressed(KeyEvent e) {
			System.out.println("按下：" + e.getKeyCode());
			plane.addDirection(e, true);
		}
		
		public void keyReleased(KeyEvent e) {
			System.out.println("释放：" + e.getKeyCode());
			plane.addDirection(e, false);
		}
	}
}
